package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.leftmenu.user;


import br.edu.ufcg.lsd.wbis.ui.client.beans.UserMatcher;
import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo.UserPermission;
import br.edu.ufcg.lsd.wbis.ui.client.beans.UserMatcher.StringMatchingType;
import br.edu.ufcg.lsd.wbis.ui.client.ui.FormManager;

import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;

/**
 * @author Renato Miceli
 */
public class SearchFormPanel extends FormPanel implements FormManager<UserMatcher> {

	private static String NAME_FIELD = "name";

	private static String LOGIN_FIELD = "login";

	private static String EMAIL_FIELD = "email";
	
	private static final String AUTHORIZED_FIELD = "authSearch";

	private static final String TYPE_FIELD = "type";

	private static final String NAME_SEARCH_TYPE_FIELD = "nameSearch";

	private static final String LOGIN_SEARCH_TYPE_FIELD = "loginSearch";

	private static final String EMAIL_SEARCH_TYPE_FIELD = "emailSearch";

	private static final String NAME_SENSITIVE_FIELD = "nameSensitive";

	private static final String LOGIN_SENSITIVE_FIELD = "loginSensitive";
	
	private static final String EMAIL_SENSITIVE_FIELD = "emailSensitive";
	
	public SearchFormPanel() {
		this.setBaseCls("x-plain");
		this.setLabelWidth(100);
		this.setWidth(400);
		
		this.setHeight(400);

		final TextField nameField = new TextField("Name", NAME_FIELD);
		this.add(nameField, new AnchorLayoutData("90%"));
		
		Checkbox nameSensitiveBox = new Checkbox("Sensitive case", NAME_SENSITIVE_FIELD);  
		nameSensitiveBox.setChecked(true);
		this.add(nameSensitiveBox, new AnchorLayoutData("90%"));
		
		ComboBox nameSearchMethodBox = new ComboBox("Name search method", NAME_SEARCH_TYPE_FIELD);
		SearchTypeStore store = new SearchTypeStore(NAME_SEARCH_TYPE_FIELD);
		nameSearchMethodBox.setStore(store);
		nameSearchMethodBox.setDisplayField(store.getColumnName());
		nameSearchMethodBox.setTypeAhead(true);
		nameSearchMethodBox.setMode(ComboBox.LOCAL);
		nameSearchMethodBox.setTriggerAction(ComboBox.ALL);
		nameSearchMethodBox.setSelectOnFocus(true);
		nameSearchMethodBox.setValue(SearchTypeStore.getValue(StringMatchingType.DEFAULT));
		this.add(nameSearchMethodBox, new AnchorLayoutData("90%"));

		final TextField loginField = new TextField("Login", LOGIN_FIELD);
		this.add(loginField, new AnchorLayoutData("70%"));

		Checkbox loginSensitiveBox = new Checkbox("Sensitive case", LOGIN_SENSITIVE_FIELD);  
		loginSensitiveBox.setChecked(true);
		this.add(loginSensitiveBox, new AnchorLayoutData("90%"));
		
		ComboBox loginSearchMethodBox = new ComboBox("Login search method", LOGIN_SEARCH_TYPE_FIELD);
		store = new SearchTypeStore(LOGIN_SEARCH_TYPE_FIELD);
		loginSearchMethodBox.setStore(store);
		loginSearchMethodBox.setDisplayField(store.getColumnName());
		loginSearchMethodBox.setTypeAhead(true);
		loginSearchMethodBox.setMode(ComboBox.LOCAL);
		loginSearchMethodBox.setTriggerAction(ComboBox.ALL);
		loginSearchMethodBox.setSelectOnFocus(true);
		loginSearchMethodBox.setValue(SearchTypeStore.getValue(StringMatchingType.DEFAULT));
		this.add(loginSearchMethodBox, new AnchorLayoutData("90%"));

		final TextField emailField = new TextField("E-mail", EMAIL_FIELD);
		this.add(emailField, new AnchorLayoutData("90%"));

		Checkbox emailSensitiveBox = new Checkbox("Sensitive case", EMAIL_SENSITIVE_FIELD);  
		emailSensitiveBox.setChecked(true);
		this.add(emailSensitiveBox, new AnchorLayoutData("90%"));
		
		ComboBox emailSearchMethodBox = new ComboBox("E-mail search method", EMAIL_SEARCH_TYPE_FIELD);
		store = new SearchTypeStore(EMAIL_SEARCH_TYPE_FIELD);
		emailSearchMethodBox.setStore(store);
		emailSearchMethodBox.setDisplayField(store.getColumnName());
		emailSearchMethodBox.setTypeAhead(true);
		emailSearchMethodBox.setMode(ComboBox.LOCAL);
		emailSearchMethodBox.setTriggerAction(ComboBox.ALL);
		emailSearchMethodBox.setSelectOnFocus(true);
		emailSearchMethodBox.setValue(SearchTypeStore.getValue(StringMatchingType.DEFAULT));
		this.add(emailSearchMethodBox, new AnchorLayoutData("90%"));
		
		ComboBox userLevelBox = new ComboBox("User type", TYPE_FIELD);
		userLevelBox.setStore(new UserLevelSearchStore());
		userLevelBox.setDisplayField(UserLevelSearchStore.getColumnName());
		userLevelBox.setTypeAhead(true);
		userLevelBox.setMode(ComboBox.LOCAL);
		userLevelBox.setTriggerAction(ComboBox.ALL);
		userLevelBox.setSelectOnFocus(true);
		userLevelBox.setValue(UserLevelSearchStore.getValue(null));
		this.add(userLevelBox, new AnchorLayoutData("70%"));

		ComboBox authorizationStatusBox = new ComboBox("Authorization status", AUTHORIZED_FIELD);
		authorizationStatusBox.setStore(new AuthorizationSearchStore());
		authorizationStatusBox.setDisplayField(AuthorizationSearchStore.getColumnName());
		authorizationStatusBox.setTypeAhead(true);
		authorizationStatusBox.setMode(ComboBox.LOCAL);
		authorizationStatusBox.setTriggerAction(ComboBox.ALL);
		authorizationStatusBox.setSelectOnFocus(true);
		authorizationStatusBox.setValue(AuthorizationSearchStore.getValue(null));
		this.add(authorizationStatusBox, new AnchorLayoutData("70%"));
		
		nameField.focus();
	}

	/**
	 * {@inheritDoc}
	 */
	public UserMatcher parseForm() {
		String name = this.getForm().findField(NAME_FIELD).getValueAsString();
		StringMatchingType nameSearchType = SearchTypeStore.getMatchingType(this.getForm().findField(NAME_SEARCH_TYPE_FIELD).getValueAsString());
		Boolean nameSensitive = this.getForm().findField(NAME_SENSITIVE_FIELD).getValueAsString().equals("true")? true : false;
		
		String login = this.getForm().findField(LOGIN_FIELD).getValueAsString().trim();
		StringMatchingType loginSearchType = SearchTypeStore.getMatchingType(this.getForm().findField(LOGIN_SEARCH_TYPE_FIELD).getValueAsString());
		Boolean loginSensitive = this.getForm().findField(LOGIN_SENSITIVE_FIELD).getValueAsString().equals("true")? true : false;
		
		String email = this.getForm().findField(EMAIL_FIELD).getValueAsString().trim();
		StringMatchingType emailSearchType = SearchTypeStore.getMatchingType(this.getForm().findField(EMAIL_SEARCH_TYPE_FIELD).getValueAsString());
		Boolean emailSensitive = this.getForm().findField(EMAIL_SENSITIVE_FIELD).getValueAsString().equals("true")? true : false;
		
		UserPermission level = UserLevelSearchStore.getPermission(this.getForm().findField(TYPE_FIELD).getValueAsString());
		Boolean authorization = AuthorizationSearchStore.getAuthorization(this.getForm().findField(AUTHORIZED_FIELD).getValueAsString());
		
		
		
		return new UserMatcher().setName(name).setNameSearchType(nameSearchType).setNameSensitive(nameSensitive)
					.setLogin(login).setLoginSearchType(loginSearchType).setLoginSensitive(loginSensitive).setEmail(email).setEmailSensitive(emailSensitive)
					.setEmailSearchType(emailSearchType).setUserLevel(level).setAuthorized(authorization);
	}
}
